<template>
  <el-dialog title="分配角色" :visible="showRoleDialog" @close="$emit('update:show-role-dialog',false)">
    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="saveRole">确定</el-button>
        <el-button size="small" @click="$emit('update:show-role-dialog',false)">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetail } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    // 用户的id 用来查询当前用户的角色信息
    // 根据当前用户的id查询出来当前用户所有的信息,其中就会有角色信息
    userId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      list: [],
      roleIds: []// 选中的数组
    }
  },
  created() {
    // console.log(this.userId)
    this.getRoleList()
    this.getUserDetail()
  },
  methods: {
    async getRoleList() {
      const res = await getRoleList({
        page: 1,
        pagesize: 50
      })
      this.list = res.rows
      console.log(this.list)
    },
    async getUserDetail() {
      const res = await getUserDetail(this.userId)
      console.log(res)
      this.roleIds = res.roleIds
    },
    async saveRole() {
      await assignRoles({ id: this.userId, roleIds: this.roleIds })
      // 关闭弹出层
      this.$emit('update:show-role-dialog', false)
    }
  }
}
</script>
